/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2022-12-02 17:04:51
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-07 11:00:55
 * @FilePath: \cxl-h5\src\main.ts
 */
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createPinia } from 'pinia'
import { PullRefresh, List, Field } from 'vant'

import router from '@/router/index'
import dialogVue from '@/components/dialog/Index.vue'
import hotline from '@/components/hotLine.vue'
import cxlSelect from '@/components/select/Index.vue'
import cxlButton from '@/components/button/Index.vue'
import cxlCheckbox from '@/components/checkbox/Index.vue'
import validateInput from '@/components/validateInput/Index.vue'

const app = createApp(App)
const pinia = createPinia()

app.use(pinia).use(router).use(PullRefresh).use(List).use(Field).mount('#app')

//全局组件注册
app.component('DialogVue', dialogVue)
app.component('hot-line', hotline)
app.component('cxl-select', cxlSelect)
app.component('cxl-button', cxlButton)
app.component('cxl-checkbox', cxlCheckbox)
app.component('cxl-input', validateInput)

console.log('当前环境', import.meta.env.VITE_MODE, import.meta.env.VITE_HOST)

//全局自定义指令
app.directive('preventRepeatClick', {
    mounted(el, binding) {
        el.addEventListener('click', () => {
            if (!el.disabled) {
                el.disabled = true
                setTimeout(() => {
                    el.disabled = false
                }, binding.value || 3000)
            }
        })
    }
})

//解决ios输入框弹出的收起，底部空白问题
const windowHeight = window.innerHeight
app.directive('fixedInput', function (el) {
    el.addEventListener('blur', function () {
        const windowFocusHeight = window.innerHeight
        if (windowHeight == windowFocusHeight) {
            return
        }
        console.log(windowHeight + '--' + windowFocusHeight)
        console.log('修复')
        let currentPosition
        const speed = 1 //页面滚动距离
        currentPosition = document.documentElement.scrollTop || document.body.scrollTop
        currentPosition -= speed
        window.scrollTo(0, currentPosition) //页面向上滚动
        currentPosition += speed //speed变量
        window.scrollTo(0, currentPosition) //页面向下滚动
    })
})
